﻿using System;
using HslCommunication.BasicFramework;

namespace HslCommunication.Profinet.Omron
{
	/// <summary>
	/// Omron PLC的FINS协议相关的辅助类，主要是一些地址解析，读写的指令生成。<br />
	/// The auxiliary classes related to the FINS protocol of Omron PLC are mainly some address resolution and the generation of read and write instructions.
	/// </summary>
	// Token: 0x0200005B RID: 91
	public class OmronFinsNetHelper
	{
		/// <summary>
		/// 解析欧姆龙的数据地址，参考来源是Omron手册第188页，比如D100， E1.100<br />
		/// Analyze Omron's data address, the reference source is page 188 of the Omron manual, such as D100, E1.100
		/// </summary>
		/// <param name="address">数据地址</param>
		/// <param name="isBit">是否是位地址</param>
		/// <returns>解析后的结果地址对象</returns>
		// Token: 0x0600079A RID: 1946 RVA: 0x00027D4C File Offset: 0x00025F4C
		public static OperateResult<OmronFinsDataType, byte[]> AnalysisAddress(string address, bool isBit)
		{
			OperateResult<OmronFinsDataType, byte[]> operateResult = new OperateResult<OmronFinsDataType, byte[]>();
			try
			{
				char c = address[0];
				char c2 = c;
				if (c2 <= 'W')
				{
					switch (c2)
					{
					case 'A':
						goto IL_C9;
					case 'B':
					case 'F':
					case 'G':
						goto IL_151;
					case 'C':
						goto IL_93;
					case 'D':
						break;
					case 'E':
						goto IL_DB;
					case 'H':
						goto IL_B7;
					default:
						if (c2 != 'W')
						{
							goto IL_151;
						}
						goto IL_A5;
					}
				}
				else
				{
					switch (c2)
					{
					case 'a':
						goto IL_C9;
					case 'b':
					case 'f':
					case 'g':
						goto IL_151;
					case 'c':
						goto IL_93;
					case 'd':
						break;
					case 'e':
						goto IL_DB;
					case 'h':
						goto IL_B7;
					default:
						if (c2 != 'w')
						{
							goto IL_151;
						}
						goto IL_A5;
					}
				}
				operateResult.Content1 = OmronFinsDataType.DM;
				goto IL_161;
				IL_93:
				operateResult.Content1 = OmronFinsDataType.CIO;
				goto IL_161;
				IL_A5:
				operateResult.Content1 = OmronFinsDataType.WR;
				goto IL_161;
				IL_B7:
				operateResult.Content1 = OmronFinsDataType.HR;
				goto IL_161;
				IL_C9:
				operateResult.Content1 = OmronFinsDataType.AR;
				goto IL_161;
				IL_DB:
				string[] array = address.Split(new char[]
				{
					'.'
				}, StringSplitOptions.RemoveEmptyEntries);
				int num = Convert.ToInt32(array[0].Substring(1), 16);
				bool flag = num < 16;
				if (flag)
				{
					operateResult.Content1 = new OmronFinsDataType((byte)(32 + num), (byte)(160 + num));
				}
				else
				{
					operateResult.Content1 = new OmronFinsDataType((byte)(224 + num - 16), (byte)(96 + num - 16));
				}
				goto IL_161;
				IL_151:
				throw new Exception(StringResources.Language.NotSupportedDataType);
				IL_161:
				bool flag2 = address[0] == 'E' || address[0] == 'e';
				if (flag2)
				{
					string[] array2 = address.Split(new char[]
					{
						'.'
					}, StringSplitOptions.RemoveEmptyEntries);
					if (isBit)
					{
						ushort value = ushort.Parse(array2[1]);
						operateResult.Content2 = new byte[3];
						operateResult.Content2[0] = BitConverter.GetBytes(value)[1];
						operateResult.Content2[1] = BitConverter.GetBytes(value)[0];
						bool flag3 = array2.Length > 2;
						if (flag3)
						{
							operateResult.Content2[2] = byte.Parse(array2[2]);
							bool flag4 = operateResult.Content2[2] > 15;
							if (flag4)
							{
								throw new Exception(StringResources.Language.OmronAddressMustBeZeroToFifteen);
							}
						}
					}
					else
					{
						ushort value2 = ushort.Parse(array2[1]);
						operateResult.Content2 = new byte[3];
						operateResult.Content2[0] = BitConverter.GetBytes(value2)[1];
						operateResult.Content2[1] = BitConverter.GetBytes(value2)[0];
					}
				}
				else if (isBit)
				{
					string[] array3 = address.Substring(1).Split(new char[]
					{
						'.'
					}, StringSplitOptions.RemoveEmptyEntries);
					ushort value3 = ushort.Parse(array3[0]);
					operateResult.Content2 = new byte[3];
					operateResult.Content2[0] = BitConverter.GetBytes(value3)[1];
					operateResult.Content2[1] = BitConverter.GetBytes(value3)[0];
					bool flag5 = array3.Length > 1;
					if (flag5)
					{
						operateResult.Content2[2] = byte.Parse(array3[1]);
						bool flag6 = operateResult.Content2[2] > 15;
						if (flag6)
						{
							throw new Exception(StringResources.Language.OmronAddressMustBeZeroToFifteen);
						}
					}
				}
				else
				{
					ushort value4 = ushort.Parse(address.Substring(1));
					operateResult.Content2 = new byte[3];
					operateResult.Content2[0] = BitConverter.GetBytes(value4)[1];
					operateResult.Content2[1] = BitConverter.GetBytes(value4)[0];
				}
			}
			catch (Exception ex)
			{
				operateResult.Message = ex.Message;
				return operateResult;
			}
			operateResult.IsSuccess = true;
			return operateResult;
		}

		/// <summary>
		/// 根据读取的地址，长度，是否位读取创建Fins协议的核心报文<br />
		/// According to the read address, length, whether to read the core message that creates the Fins protocol
		/// </summary>
		/// <param name="address">地址，具体格式请参照示例说明</param>
		/// <param name="length">读取的数据长度</param>
		/// <param name="isBit">是否使用位读取</param>
		/// <returns>带有成功标识的Fins核心报文</returns>
		// Token: 0x0600079B RID: 1947 RVA: 0x000280E8 File Offset: 0x000262E8
		public static OperateResult<byte[]> BuildReadCommand(string address, ushort length, bool isBit)
		{
			OperateResult<OmronFinsDataType, byte[]> operateResult = OmronFinsNetHelper.AnalysisAddress(address, isBit);
			bool flag = !operateResult.IsSuccess;
			OperateResult<byte[]> result;
			if (flag)
			{
				result = OperateResult.CreateFailedResult<byte[]>(operateResult);
			}
			else
			{
				byte[] array = new byte[8];
				array[0] = 1;
				array[1] = 1;
				if (isBit)
				{
					array[2] = operateResult.Content1.BitCode;
				}
				else
				{
					array[2] = operateResult.Content1.WordCode;
				}
				operateResult.Content2.CopyTo(array, 3);
				array[6] = (byte)(length / 256);
				array[7] = (byte)(length % 256);
				result = OperateResult.CreateSuccessResult<byte[]>(array);
			}
			return result;
		}

		/// <summary>
		/// 根据写入的地址，数据，是否位写入生成Fins协议的核心报文<br />
		/// According to the written address, data, whether the bit is written to generate the core message of the Fins protocol
		/// </summary>
		/// <param name="address">地址内容，具体格式请参照示例说明</param>
		/// <param name="value">实际的数据</param>
		/// <param name="isBit">是否位数据</param>
		/// <returns>带有成功标识的Fins核心报文</returns>
		// Token: 0x0600079C RID: 1948 RVA: 0x00028178 File Offset: 0x00026378
		public static OperateResult<byte[]> BuildWriteWordCommand(string address, byte[] value, bool isBit)
		{
			OperateResult<OmronFinsDataType, byte[]> operateResult = OmronFinsNetHelper.AnalysisAddress(address, isBit);
			bool flag = !operateResult.IsSuccess;
			OperateResult<byte[]> result;
			if (flag)
			{
				result = OperateResult.CreateFailedResult<byte[]>(operateResult);
			}
			else
			{
				byte[] array = new byte[8 + value.Length];
				array[0] = 1;
				array[1] = 2;
				if (isBit)
				{
					array[2] = operateResult.Content1.BitCode;
				}
				else
				{
					array[2] = operateResult.Content1.WordCode;
				}
				operateResult.Content2.CopyTo(array, 3);
				if (isBit)
				{
					array[6] = (byte)(value.Length / 256);
					array[7] = (byte)(value.Length % 256);
				}
				else
				{
					array[6] = (byte)(value.Length / 2 / 256);
					array[7] = (byte)(value.Length / 2 % 256);
				}
				value.CopyTo(array, 8);
				result = OperateResult.CreateSuccessResult<byte[]>(array);
			}
			return result;
		}

		/// <summary>
		/// 验证欧姆龙的Fins-TCP返回的数据是否正确的数据，如果正确的话，并返回所有的数据内容<br />
		/// Verify that the data returned by Omron's Fins-TCP is correct data, if correct, and return all data content
		/// </summary>
		/// <param name="response">来自欧姆龙返回的数据内容</param>
		/// <param name="isRead">是否读取</param>
		/// <returns>带有是否成功的结果对象</returns>
		// Token: 0x0600079D RID: 1949 RVA: 0x00028248 File Offset: 0x00026448
		public static OperateResult<byte[]> ResponseValidAnalysis(byte[] response, bool isRead)
		{
			bool flag = response.Length >= 16;
			OperateResult<byte[]> result;
			if (flag)
			{
				int num = BitConverter.ToInt32(new byte[]
				{
					response[15],
					response[14],
					response[13],
					response[12]
				}, 0);
				bool flag2 = num > 0;
				if (flag2)
				{
					result = new OperateResult<byte[]>(num, OmronFinsNetHelper.GetStatusDescription(num));
				}
				else
				{
					byte[] array = new byte[response.Length - 16];
					Array.Copy(response, 16, array, 0, array.Length);
					result = OmronFinsNetHelper.UdpResponseValidAnalysis(array, isRead);
				}
			}
			else
			{
				result = new OperateResult<byte[]>(StringResources.Language.OmronReceiveDataError);
			}
			return result;
		}

		/// <summary>
		/// 验证欧姆龙的Fins-Udp返回的数据是否正确的数据，如果正确的话，并返回所有的数据内容<br />
		/// Verify that the data returned by Omron's Fins-Udp is correct data, if correct, and return all data content
		/// </summary>
		/// <param name="response">来自欧姆龙返回的数据内容</param>
		/// <param name="isRead">是否读取</param>
		/// <returns>带有是否成功的结果对象</returns>
		// Token: 0x0600079E RID: 1950 RVA: 0x000282E4 File Offset: 0x000264E4
		public static OperateResult<byte[]> UdpResponseValidAnalysis(byte[] response, bool isRead)
		{
			bool flag = response.Length >= 14;
			OperateResult<byte[]> result;
			if (flag)
			{
				int num = (int)response[12] * 256 + (int)response[13];
				bool flag2 = !isRead;
				if (flag2)
				{
					OperateResult<byte[]> operateResult = OperateResult.CreateSuccessResult<byte[]>(new byte[0]);
					operateResult.ErrorCode = num;
					operateResult.Message = OmronFinsNetHelper.GetStatusDescription(num) + " Received:" + SoftBasic.ByteToHexString(response, ' ');
					result = operateResult;
				}
				else
				{
					byte[] array = new byte[response.Length - 14];
					bool flag3 = array.Length != 0;
					if (flag3)
					{
						Array.Copy(response, 14, array, 0, array.Length);
					}
					OperateResult<byte[]> operateResult2 = OperateResult.CreateSuccessResult<byte[]>(array);
					bool flag4 = array.Length == 0;
					if (flag4)
					{
						operateResult2.IsSuccess = false;
					}
					operateResult2.ErrorCode = num;
					operateResult2.Message = OmronFinsNetHelper.GetStatusDescription(num) + " Received:" + SoftBasic.ByteToHexString(response, ' ');
					result = operateResult2;
				}
			}
			else
			{
				result = new OperateResult<byte[]>(StringResources.Language.OmronReceiveDataError);
			}
			return result;
		}

		/// <summary>
		/// 根据欧姆龙返回的错误码，获取错误信息的字符串描述文本<br />
		/// According to the error code returned by Omron, get the string description text of the error message
		/// </summary>
		/// <param name="err">错误码</param>
		/// <returns>文本描述</returns>
		// Token: 0x0600079F RID: 1951 RVA: 0x000283E4 File Offset: 0x000265E4
		public static string GetStatusDescription(int err)
		{
			string result;
			switch (err)
			{
			case 0:
				result = StringResources.Language.OmronStatus0;
				break;
			case 1:
				result = StringResources.Language.OmronStatus1;
				break;
			case 2:
				result = StringResources.Language.OmronStatus2;
				break;
			case 3:
				result = StringResources.Language.OmronStatus3;
				break;
			default:
				switch (err)
				{
				case 20:
					result = StringResources.Language.OmronStatus20;
					break;
				case 21:
					result = StringResources.Language.OmronStatus21;
					break;
				case 22:
					result = StringResources.Language.OmronStatus22;
					break;
				case 23:
					result = StringResources.Language.OmronStatus23;
					break;
				case 24:
					result = StringResources.Language.OmronStatus24;
					break;
				case 25:
					result = StringResources.Language.OmronStatus25;
					break;
				default:
					result = StringResources.Language.UnknownError;
					break;
				}
				break;
			}
			return result;
		}
	}
}
